package com.ohos.connect.devicecloud.iot.modular.pro.utils;


import com.ohos.connect.devicecloud.common.pojo.CommonResult;
import com.ohos.connect.devicecloud.dev.api.DevConfigApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: ChenJiaHao
 * @Date: 2024/6/5 14:21
 * @Description:
 */
@Slf4j
@Component
public class RestTemplateHelper {

    @Resource(name = "defaultRestTemplate")
    private RestTemplate restTemplate;

    @Resource
    private DevConfigApi devConfigApi;

    private final String BUSINESS_PRODUCT_DETAIL_URL = "BUSINESS_PRODUCT_DETAIL_URL";
    private final String BUSINESS_PRODUCT_PROFILE_URL = "BUSINESS_PRODUCT_PROFILE_URL";
    private final String ACCOUNT_LOGIN_URL = "ACCOUNT_LOGIN_URL";
    private final String ACCOUNT_LOGIN_USER = "ACCOUNT_LOGIN_USER";
    private final String ACCOUNT_LOGIN_PASSWORD = "ACCOUNT_LOGIN_PASSWORD";
    public CommonResult getProductDetail(String prodId) {

        String token = getToken();
        String path = devConfigApi.getValueByKey(BUSINESS_PRODUCT_DETAIL_URL);
        if (StringUtils.isEmpty(path)){
            log.error(">>> 未配置产品详情接口地址");
            return CommonResult.error("未配置产品详情接口");
        } else {
            path = path + "?prodId=" + prodId;
        }
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("token", token);
        // 请求体

        //构造实体对象
        HttpEntity<String> httpEntity = new HttpEntity<>("parameters",headers);
        try {
            ResponseEntity<CommonResult> responseEntity = restTemplate.exchange(path, HttpMethod.GET, httpEntity, CommonResult.class);
            HttpStatus httpStatus = responseEntity.getStatusCode();
            if (httpStatus.equals(HttpStatus.OK)) {
                return responseEntity.getBody();
            }

            log.error(">>> 调用远程服务接口失败，响应结果：{}", responseEntity);
            return CommonResult.error("请求接口失败 :" + path);
        } catch (Exception e) {
            return CommonResult.error("调用远程服务器时发生异常");
        }
    }

    public CommonResult getProFile(String fileId) {
        String token = getToken();
        String path = devConfigApi.getValueByKey(BUSINESS_PRODUCT_PROFILE_URL);
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("token", token);

        // 请求体
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("fileId", fileId);

        //构造实体对象
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(requestBody, headers);
        try {
            ResponseEntity<CommonResult> responseEntity = restTemplate.postForEntity(path,httpEntity,CommonResult.class);
            HttpStatus httpStatus = responseEntity.getStatusCode();
            if (httpStatus.equals(HttpStatus.OK)) {
                return responseEntity.getBody();
            }

            log.error(">>> 调用远程服务接口失败，响应结果：{}", responseEntity);
            return CommonResult.error("请求接口失败 :" + path);
        } catch (Exception e) {
            return CommonResult.error("调用远程服务器时发生异常");
        }
    }

    public String getToken() {
        String accountLoginUrl = devConfigApi.getValueByKey(ACCOUNT_LOGIN_URL);
        String accountLoginUser = devConfigApi.getValueByKey(ACCOUNT_LOGIN_USER);
        String accountLoginPassword = devConfigApi.getValueByKey(ACCOUNT_LOGIN_PASSWORD);
        if (StringUtils.isEmpty(accountLoginUrl) || StringUtils.isEmpty(accountLoginUser) || StringUtils.isEmpty(accountLoginPassword)) {
            return StringUtils.EMPTY;
        }
        // 请求体
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("account", accountLoginUser);
        requestBody.put("password", accountLoginPassword);
        //构造实体对象
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(requestBody);
        try {
            ResponseEntity<CommonResult> responseEntity = restTemplate.postForEntity(accountLoginUrl, httpEntity, CommonResult.class);
            HttpStatus httpStatus = responseEntity.getStatusCode();
            if (httpStatus.equals(HttpStatus.OK)) {
                return Objects.requireNonNull(responseEntity.getBody()).getData().toString();
            }
            log.error(">>> 调用远程服务接口失败，响应结果：{}", responseEntity);
            return StringUtils.EMPTY;
        } catch (Exception e) {
            log.error(">>> 调用远程服务器时发生异常",e);
            return StringUtils.EMPTY;
        }
    }

}
